<template>
  <div class="data-table-container">
    <el-table :data="tableData" border style="width: 100%" v-loading="loading">
      <el-table-column prop="politeName" label="活动名称" min-width="150" />

      <el-table-column label="起止时间" width="180">
        <template #default="scope">
          {{ scope.row.time }}
        </template>
      </el-table-column>
      <el-table-column prop="remark" label="备注" min-width="150" />
      <el-table-column prop="state" label="活动状态" width="120">
        <template #default="scope">
          <el-tag :type="getStatusType(scope.row.state)">
            {{ getStatusText(scope.row.state) }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="160">
        <template #default="scope">
          <el-button type="primary" link @click="handleView(scope.row)">详情</el-button>
          <el-button type="success" link @click="handleEdit(scope.row)">编辑</el-button>
          <el-button type="danger" link @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <div class="pagination-container">
      <el-pagination
        :current-page="currentPage"
        :page-size="pageSize"
        :page-sizes="[10, 20, 50, 100]"
        :total="total"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
  </div>
</template>

<script setup lang="ts">
  import { ref, watch } from 'vue'

  // 定义props
  const props = defineProps({
    tableData: {
      type: Array,
      default: () => []
    },
    loading: {
      type: Boolean,
      default: false
    },
    total: {
      type: Number,
      default: 0
    }
  })

  // 定义emits
  const emit = defineEmits(['view', 'edit', 'delete', 'size-change', 'current-change'])

  // 获取状态文本
  const getStatusText = (state: number) => {
    switch (state) {
      case 0:
        return '未开始'
      case 1:
        return '进行中'
      case 2:
        return '已结束'
      default:
        return '未知状态'
    }
  }

  // 获取状态类型
  const getStatusType = (state: number) => {
    switch (state) {
      case 0:
        return '' // 未开始
      case 1:
        return 'primary' // 进行中
      case 2:
        return 'info' // 已结束
      default:
        return 'info'
    }
  }

  // 分页相关
  const currentPage = ref(1)
  const pageSize = ref(10)

  // 处理查看详情事件
  const handleView = (row: any) => {
    emit('view', row)
  }

  // 处理编辑事件
  const handleEdit = (row: any) => {
    emit('edit', row)
  }

  // 处理删除事件
  const handleDelete = (row: any) => {
    emit('delete', row)
  }

  // 处理分页大小变化
  const handleSizeChange = (val: number) => {
    pageSize.value = val
    emit('size-change', val)
  }

  // 处理当前页变化
  const handleCurrentChange = (val: number) => {
    currentPage.value = val
    emit('current-change', val)
  }

  // 监听total变化，重置当前页
  watch(
    () => props.total,
    (newVal, oldVal) => {
      if (newVal !== oldVal) {
        currentPage.value = 1
      }
    }
  )
</script>

<style scoped lang="scss">
  .data-table-container {
    .pagination-container {
      margin-top: 20px;
      display: flex;
      justify-content: flex-end;
    }
  }
</style>
